﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015-2018 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Image Photo Style</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="photo style for OL3 marker." />
  <meta name="keywords" content="ol3, style, vector, photo, image" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
  
  <!-- ol-ext -->
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <link rel="stylesheet" href="../style.css" />
  <style>
    .options { max-width:400px;  }
    .options p { margin:0; font-size:0.9em; }
    .options img { max-width: 300px; max-height: 300px; }
    .options p.copy {
      font-size: 0.8em;
      color: navy;
    }
  </style>
</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Image Photo Style</h1>
  </a>
  <p class="info">
    The <i>ol.style.Photo</i> is a style to show photos or images on a map.
    <br/>
    The photo are drawn in a box, a circle and can be anchored.
  </p>

  <!-- DIV pour la carte -->
  <div id="map" style="width:600px; height:400px;"></div>
  <div class="options">
    <h2>Options:</h2>
    <ul>
      <li>
        kind:
        <select id="kind" onchange="vector.changed();">
          <option value="default">default</option>
          <option value="square">square</option>
          <option value="circle">circle</option>
          <option value="anchored">anchored</option>
          <option value="folio">folio</option>
        </select>
      </li>
      <li>
        border:
        <input id="border" type="number" onchange="vector.changed();" value="3" style="width:4em" />
      </li>
      <li>
        <input id="shadow" type="checkbox" checked="checked" onchange="vector.changed();" />
        <label for="shadow"> shadow</label>
      </li>
      <li>
        <input id="crop" type="checkbox" checked="checked" onchange="vector.changed();" />
        <label for="crop"> crop image within square</label>
      </li>
    </ul>
  </div>
  <div id ="select" class="options" >Select an image.</div>
  
  <script type="text/javascript">
  // Layers
  var layer = new ol.layer.Tile({ source: new ol.source.StadiaMaps({ layer: 'stamen_watercolor' }) });
    
  // The map
  var map = new ol.Map ({
    target: 'map',
    view: new ol.View ({
      zoom: 16,
      center: [260767, 6250718]
    }),
    layers: [layer]
  });

  // Array to cache image style
  var styleCache = {};
  // Vector style
  function getFeatureStyle (feature, resolution, sel) {
    var k = $('#kind').val()+"_"+$("#border").val()+"_"+feature.get("img").match(/[^\\/]+$/)[0]+($("#shadow").prop('checked')?"_1":"_0")+($("#crop").prop('checked')?"_1":"_0")+(sel?"_1":"");
    var style = styleCache[k];
    if (!style) {
      styleCache[k] = style = new ol.style.Style ({
        image: new ol.style.Photo ({
          src: feature.get("img"),
          radius: 20,
          crop: $("#crop").prop('checked'),
          kind: $('#kind').val(),
          shadow: $("#shadow").prop('checked')?5:0,
          onload: function() { vector.changed(); },
          displacement: [0, $('#kind').val() === 'anchored' ? 20 : 0],
          stroke: new ol.style.Stroke({
            width: Number($("#border").val()) + (sel ? 3 : 0),
            color: sel ? 'red' : '#fff'
          })
        })
      });
    }
    return [style];
  }

  // GeoJSON layer
  var vectorSource = new ol.source.Vector({
    url: '../data/paris-1900.geojson',
    projection: 'EPSG:3857',
    format: new ol.format.GeoJSON(),
    attributions: [ "&copy; <a href='https://twitter.com/search?q=paris%20autrefois%20(from%3ASamuelMartin75)&src=typed_query&f=live'>@SamuelMartin75</a>" ]
  });

  var vector = new ol.layer.Vector({
    name: 'Paris 1900',
    preview: "https://pbs.twimg.com/media/Fnnx0ylXgAABNjV?format=jpg&name=small",
    source: vectorSource,
    // y ordering
    renderOrder: ol.ordering.yOrdering(),
    style: getFeatureStyle
  });

  map.addLayer(vector);

  // Control Select 
  var select = new ol.interaction.Select({
    condition: ol.events.condition.click,
    style: function (feature, resolution) { 
      return getFeatureStyle(feature, resolution, true); 
    }
  })
  map.addInteraction(select);
  
  // onselect
  select.getFeatures().on(['add','remove'], function(e) {
    if (e.type=="add") {
      var info = $("#select").html("<p>Selection:</p>");
      var el = e.element;
      $("<h3>").text(el.get("title")).appendTo(info);
      $("<img>").attr('src',el.get("img")).appendTo(info);
      $("<p>").text(el.get("description")).appendTo(info);
      $("<p>").addClass('copy').html("&copy; "+el.get("copy")).appendTo(info);
    }
    else $("#select").html("<p>Select an image.</p>");
  });

  </script>
</body>
</html>